<!-- component-name -->
<template>
  <div class="wrapper">
    <el-form :inline="true" class="demo-form-inline">
        <el-form-item>
              <el-input v-model="searchObj.hosname" placeholder="医院名称" />
      </el-form-item>
        <el-form-item>
            <el-input v-model="searchObj.hoscode" placeholder="医院编号" />
      </el-form-item>
      <el-button type="primary" icon="el-icon-search" @click="fetchData()"
        >查询</el-button
      >
      <el-button type="default" @click="resetSearchObj">清空</el-button>
    </el-form>

    <!-- 工具条 -->
    <div>
       <el-button type="danger" size="mini" @click="batchDelete()" :disabled="selectedIdList.length == 0">批量删除</el-button>
    </div>

    <!-- banner列表 -->
    <el-table :data="list" stripe style="width: 100%" row-key="id" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" />
      <el-table-column type="index" width="100" label="序号" />
      <el-table-column prop="hosname" label="医院名称" />
      <el-table-column prop="hoscode" label="医院编号" />
      <el-table-column prop="apiUrl" label="api基础路径" width="200" />
      <el-table-column prop="contactsName" label="联系人姓名" />
      <el-table-column prop="contactsPhone" label="联系人手机" />
      <el-table-column label="状态" width="80">
        <template slot-scope="scope">
          {{ scope.row.status === 1 ? "可用" : "不可用" }}
        </template>
      </el-table-column>
      <el-table-column label="操作" width="200" align="center">  
        <template slot-scope="scope">
          <el-button type="danger" size="mini" icon="el-icon-delete"  @click="confirmDelete(scope.row.id)"></el-button>
          <el-button type="warn" size="mini" v-if="scope.row.status == 1"  @click="changeStatus(scope.row.id, 0)">锁定</el-button>
          <el-button type="primary" size="mini" v-if="scope.row.status == 0"  @click="changeStatus(scope.row.id, 1)">解锁</el-button>
          <router-link :to="'/hospset/edit/'+scope.row.id">
            <el-button type="primary" size="mini" icon="el-icon-edit"></el-button>
          </router-link>
        </template>
      </el-table-column>
    </el-table>
    <el-pagination
      :current-page="current"
      :page-size="limit"
      :total="total"
      style="padding: 30px 0;  text-align: center"
      layout="total, prev, pager, next, jumper"
      @current-change="fetchData"
    />
  </div>
</template>

<script>
import { getList, removeById, batchRemove, lockHospitalSet } from "@/api/hospset";

export default {
  name: "component-name",
  data() {
    return {
      current: 1, // 当前页
      limit: 3, // 每页记录数
      searchObj: {}, // 查询条件
      total: 0, // 总记录数
      list: [], // 数据列表
      selectedIdList: []
    };
  },
  methods: {
    // 获取列表数据
    fetchData(page = 1) {
      this.current = page;
      getList(this.current, this.limit, this.searchObj)
        .then((response) => {
          // 请求成功
          console.log(response);
          this.total = response.data.total;
          this.list = response.data.records;
        })
        .catch((err) => {
          // 请求失败
          console.log(err);
        });
    },
    // 清空搜索条件
    resetSearchObj() {
      this.searchObj = {};
    },
    // 删除医院信息
    confirmDelete(id) {
      this.$confirm("此操作将永久删除该信息, 是否继续?", "提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning",
      }).then(() => { // 点击确认后的操作
        removeById(id)
        .then((res) => {
          this.$message({
            message: res.message,
            type: "success",
          });
          this.fetchData()
          }).catch((err) => {
          this.$message.error(err.message);
        });
      }).catch(() => {  // 点击取消后的提示
        this.$message({
          type: "info",
          message: "已取消删除",
        });
      });
    },
    handleSelectionChange(selection) {
      
      this.selectedIdList = []
      if(selection.length > 0) {
        for(let i = 0; i < selection.length; i++) {
          this.selectedIdList.push(selection[i].id)
        }
      }
      console.log(this.selectedIdList)
    },
    // 批量删除
    batchDelete() {
      let hintMessage = "一条"
      if(this.selectedIdList.length > 1) {
        hintMessage = "多条"
      } 
      this.$confirm(`此操作将删除${hintMessage}记录, 是否继续?`, "提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning",
      }).then(() => { // 点击确认后的操作
        batchRemove(this.selectedIdList)
        .then((res) => {
          this.$message({
            message: res.message,
            type: "success",
          });
          this.fetchData()
          }).catch((err) => {
          this.$message.error(err.message);
        });
      }).catch(() => {  // 点击取消后的提示
        this.$message({
          type: "info",
          message: "已取消删除",
        });
      });
    },
    // 修改状态
    changeStatus(id, status) {
      lockHospitalSet(id, status)
      this.fetchData()
    }
  },
  created() {
    this.fetchData();
  },
};
</script>
<style lang='scss' scoped>
// @import url();
.wrapper {
  padding: 20px 10px;
}
</style>
